package com.example.xd.gupmodule;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;

import jp.co.cyberagent.android.gpuimage.GPUImage;

public class MainActivity extends AppCompatActivity implements BaseQuickAdapter.OnItemClickListener {

    private RecyclerView mRv;
    ArrayList<Integer> integers = new ArrayList<>();
    private ItemAdapter mItemAdapter;
    private Bitmap mBitmap;
    private ImageView mImg_filter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ImageView img = findViewById(R.id.img);
        mImg_filter = findViewById(R.id.img_filter);
        mRv = findViewById(R.id.rv);
        for (int i = 1; i <= 86; i++) {
            integers.add(i);
        }
        mRv.setLayoutManager(new GridLayoutManager(this, 5));
        mItemAdapter = new ItemAdapter(R.layout.item, integers);
        mItemAdapter.setOnItemClickListener(this);
        mRv.setAdapter(mItemAdapter);

        mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.test_img);
        img.setImageBitmap(mBitmap);
        addFilter(mImg_filter, 1);
    }

    private void addFilter(ImageView img_filter, Integer filter) {
        GPUImage gpuImage = new GPUImage(this);
        gpuImage.setImage(mBitmap);
        gpuImage.setFilter(GPUImageUtil.getFilter(filter));
        Bitmap bitmapWithFilterApplied = gpuImage.getBitmapWithFilterApplied();
        img_filter.setImageBitmap(bitmapWithFilterApplied);
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        Integer integer = integers.get(position);
        addFilter(mImg_filter, integer);
    }
}
